//
//  SCBaseTabBarViewController.m
//  Assembly
//
//  Created by 葛悦彣 on 2023/11/9.
//

#import "SCBaseTabBarViewController.h"
#import "SCBaseNavigationViewController.h"
#import "YTStudyAllViewController.h"
#import "YTMyViewController.h"
#import "YTFishpondViewController.h"
#import "YTStashViewController.h"
//#import <Flutter/flutter.h>
#import "YTYGFishpondViewController.h"

@interface SCBaseTabBarViewController ()<UINavigationControllerDelegate>

@end

@implementation SCBaseTabBarViewController
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    navigationController.navigationBar.hidden  = YES ;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    navigationController.navigationBar.hidden  = YES ;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpAllChildViewController];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [self userInfo];
    if([UserInfoModel shared].userIdentity == 1){///管理员
        self.selectedIndex = 1;
    }
}
- (void)userInfo{
//    [SCNetManger PostmyCenterWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPDicBase * model) {
//        if (model==nil) {
//            [self.view showWarning:@"网络不可用"];
//        }else{
//            if (model.code == 200){
//                UserInfoModel * usermodel = [UserInfoModel yy_modelWithJSON:model.result];
//                [UserInfoModel shared].username = usermodel.username;
//                [UserInfoModel shared].phone = usermodel.phone;
//                [UserInfoModel shared].realname = usermodel.realname;
//                NSString * rolesStr = @"";
//                for (UserInfoRolesModel * roles in usermodel.roles) {
//                    if(rolesStr.length > 0){
//                        rolesStr = [NSString stringWithFormat:@"%@,%@", rolesStr,roles.roleCode];
//                    }  else {
//                        rolesStr = roles.roleCode;
//                    }
//                }
//                [UserInfoModel shared].rolesStr = rolesStr;
//            }
//        }
//    }];
}

-(void)setUpAllChildViewController{
    if([UserInfoModel shared].userIdentity == 3){///员工        
        [self setUpOneChildViewController:[YTYGFishpondViewController new] selctedImage:[UIImage imageNamed:@"fish_pond1"] image:[UIImage imageNamed:@"fish_pond2"] title:@"首页"];
        [self setUpOneChildViewController:[YTStudyAllViewController new] selctedImage:[UIImage imageNamed:@"study1"] image:[UIImage imageNamed:@"study2"] title:@"学习"];
    } else  {
        [self setUpOneChildViewController:[YTStudyAllViewController new] selctedImage:[UIImage imageNamed:@"study1"] image:[UIImage imageNamed:@"study2"] title:@"学习"];
        [self setUpOneChildViewController:[YTFishpondViewController new] selctedImage:[UIImage imageNamed:@"fish_pond1"] image:[UIImage imageNamed:@"fish_pond2"] title:@"池塘"];
    }
    [self setUpOneChildViewController:[YTStashViewController new] selctedImage:[UIImage imageNamed:@"warehouse1"] image:[UIImage imageNamed:@"warehouse2"] title:@"仓库"];
    [self setUpOneChildViewController:[YTMyViewController new] selctedImage:[UIImage imageNamed:@"personal_center1"] image:[UIImage imageNamed:@"personal_center2"] title:@"我的"];
}


-(void)setUpOneChildViewController:(UIViewController *)viewController selctedImage:(UIImage *)selctedImage image:(UIImage *)image title:(NSString *)title{
    SCBaseNavigationViewController *nav = [[SCBaseNavigationViewController alloc] initWithRootViewController:viewController];
    nav.title = title;
    nav.delegate = self;
    nav.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = [selctedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    nav.tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateSelected];

    [self addChildViewController:nav];
    
}

- (UIViewController *)sj_topViewController {
    if ( self.selectedIndex == NSNotFound )
        return self.viewControllers.firstObject;
    return self.selectedViewController;
}

- (BOOL)shouldAutorotate {
    return [[self sj_topViewController] shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [[self sj_topViewController] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self sj_topViewController] preferredInterfaceOrientationForPresentation];
}\
@end
